<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="remark">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="真实姓名/手机号"/>
                </el-form-item>
                <el-form-item label="" prop="status">
                    <el-select v-model="queryParams.status" class="w-[280px]" clearable placeholder="提现状态">
                        <el-option v-for="(item, index) in dictData.withdraw_status" :key="index" :label="item.name" :value="item.value"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table class="mt-4" size="small" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="编号" prop="id" min-width="50" />
                <el-table-column label="真实姓名" prop="realName" min-width="100" />
                <el-table-column label="手机号" prop="mobile" min-width="100" />
                <el-table-column label="体现状态" prop="status" min-width="100">
                    <template #default="{ row }">
                        <dict-value :options="dictData.withdraw_status" :value="row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="申请时间" prop="createTime" min-width="100" />
                <el-table-column label="提现金额" prop="amount" min-width="100" />

                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button v-if="row.status === 0" v-perms="['withdraw:edit']" type="primary" link @click="handleEditStatus(row, '通过')">
                            通过
                        </el-button>
                        <el-button v-if="row.status === 0" v-perms="['withdraw:edit']" type="danger" link @click="handleEditStatus(row, '驳回')">
                            驳回
                        </el-button>
                        <el-button v-perms="['withdraw:edit']" type="primary" link @click="handleEdit(row)">
                            详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="withdraw">
import { withdrawEditStatus, withdrawLists } from '@/api/withdraw'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    userId: '',
    amount: '',
    cardId: '',
    checkTimeStart: '',
    checkTimeEnd: '',
    checkUser: '',
    status: '',
    tradeNo: '',
    remark: '',
    keyword: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: withdrawLists,
    params: queryParams
})

const { dictData } = useDictData<{
    withdraw_status: any[]
}>(['withdraw_status'])

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleEditStatus = async (row: object, status: string) => {
    if (status === "通过") {
        await feedback.confirm('确定要通过？')
        await withdrawEditStatus({ id: row.id, status: 1})
        feedback.msgSuccess('通过成功')
    } else {
        await feedback.confirm('确定要驳回？')
        await withdrawEditStatus({ id: row.id, status: 3})
        feedback.msgSuccess('已驳回')
    }
    getLists()
}

getLists()
</script>
